<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetPluginUsages;

use App\Base\Models\BaseAdminModel;
use App\Plugins\FcpApi\TweetConfigs\TweetConfigService;
use App\Plugins\FcpApi\TweetFiles\TweetFiles;
use App\Plugins\FcpApi\TweetLanguages\FsModel as TweetLanguagesModel;
use App\Plugins\FcpApi\TweetLanguages\TweetLanguagesService;

class FsModel extends BaseAdminModel
{
    protected $table = FsConfig::CFG_TABLE;

    // 前台表单字段映射
    public function formFieldsMap()
    {
        return FsConfig::FORM_FIELDS_MAP;
    }

    // 新增搜索条件
    public function getAddedSearchableFields()
    {
        return FsConfig::ADDED_SEARCHABLE_FIELDS;
    }

    // hook-添加之后
    public function hookStoreAfter($id)
    {
        // 文件表
        if (request()->input('icon_file_id')) {
            TweetFiles::where('id', request()->input('icon_file_id'))->update(['table_type'=>1, 'table_name'=>FsConfig::CFG_TABLE, 'table_field' => 'id', 'table_id'=> $id]);
        }

        // 语言表
        $nameArr = json_decode(request()->input('name'), true);
        // 语言
        $languageArr = TweetConfigService::getLanguageStatus();
        $default_language = $languageArr['default_language'];

        $inputArr = [];
        if ($nameArr) {
            foreach ($nameArr as $v) {
                if ($default_language == $v['lang_tag']) {
                    TweetPluginUsages::where('id', $id)->update(['name'=>$v['lang_content']]);
                }
                $item = [];
                $item['lang_tag'] = $v['lang_tag'];
                $item['lang_content'] = $v['lang_content'];
                $item['table_field'] = FsConfig::FORM_FIELDS_MAP['name'];
                $item['table_id'] = $id;
                $item['table_name'] = FsConfig::CFG_TABLE;
                // $item['alias_key'] = $v['nickname'];
                $inputArr[] = $item;
            }
        }
        TweetLanguagesModel::insert($inputArr);
    }

    // hook - 编辑之后
    public function hookUpdateAfter($id)
    {
        // 文件表
        if (request()->input('icon_file_id')) {
            TweetFiles::where('id', request()->input('icon_file_id'))->update(['table_type'=>1, 'table_name'=>FsConfig::CFG_TABLE, 'table_field' => 'id', 'table_id'=> $id]);
        }
        // 语言表
        // 语言
        $languageArr = TweetConfigService::getLanguageStatus();
        $default_language = $languageArr['default_language'];
        $nameArr = json_decode(request()->input('name'), true);
        $inputArr = [];
        TweetLanguagesModel::where('table_name', FsConfig::CFG_TABLE)->where('table_field', FsConfig::FORM_FIELDS_MAP['name'])->where('table_id', $id)->delete();
        if ($nameArr) {
            foreach ($nameArr as $v) {
                if ($default_language == $v['lang_tag']) {
                    TweetPluginUsages::where('id', $id)->update(['name'=>$v['lang_content']]);
                }
                $item = [];
                $item['lang_tag'] = $v['lang_tag'];
                $item['lang_content'] = $v['lang_content'];
                $item['table_field'] = FsConfig::FORM_FIELDS_MAP['name'];
                $item['table_id'] = $id;
                $item['table_name'] = FsConfig::CFG_TABLE;
                // $item['alias_key'] = $v['nickname'];
                $inputArr[] = $item;
            }
        }
        TweetLanguagesModel::insert($inputArr);
    }
    public function initOrderByFields()
    {
        $orderByFields = [
            'rank_num' => "ASC",
            // 'updated_at' => 'DESC',
        ];

        return $orderByFields;
    }
}
